package Builder;

import javax.swing.*;
import java.awt.*;

public class ParlourDecorator {
    public static void main(String[] args) {
        try {
            Decorator d;
            d = (Decorator)ReadXML.getObject();
            ProjectManager m = new ProjectManager(d);
            Parlour p = m.decorate();
            p.show();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
//产品类Product
class Parlour{
    private String wall;
    private String TV;
    private String Sofa;

    public String getWall() {
        return wall;
    }

    public void setWall(String wall) {
        this.wall = wall;
    }

    public String getTV() {
        return TV;
    }

    public void setTV(String TV) {
        this.TV = TV;
    }

    public String getSofa() {
        return Sofa;
    }

    public void setSofa(String sofa) {
        Sofa = sofa;
    }
    //展示方法
    public void show(){
        JFrame jf=new JFrame("建造者模式测试");
        Container contentPane=jf.getContentPane();
        JPanel p=new JPanel();
        JScrollPane sp=new JScrollPane(p);
        String parlour=wall+TV+Sofa;
        JLabel l=new JLabel(new ImageIcon("src/"+parlour+".jpg"));
        p.setLayout(new GridLayout(1,1));
        p.setBorder(BorderFactory.createTitledBorder("客厅"));
        p.add(l);
        contentPane.add(sp,BorderLayout.CENTER);
        jf.pack();
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}
//抽象建造类
abstract class Decorator{
    //创建产品对象
    protected  Parlour product = new Parlour();
    //创建方法
    public abstract void buildWall();
    public abstract void buildTV();
    public abstract void buildSofa();
    //返回产品对象
    public Parlour getProduct(){
        return product;
    }
}
//具体建造类1,2
class ConcreteDecorator1 extends Decorator{

    @Override
    public void buildWall() {
        product.setWall("wall_1");
    }

    @Override
    public void buildTV() {
        product.setTV("TV_1");
    }

    @Override
    public void buildSofa() {
        product.setSofa("Sofa_1");
    }
}
class ConcreteDecorator2 extends Decorator{

    @Override
    public void buildWall() {
        product.setWall("wall_2");
    }

    @Override
    public void buildTV() {
        product.setTV("TV_2");
    }

    @Override
    public void buildSofa() {
        product.setSofa("Sofa_2");
    }
}
//指挥者：
class ProjectManager{
    private Decorator builder;
    public  ProjectManager(Decorator builder){
        this.builder = builder;
    }
    //产品构建与组装
    public Parlour decorate(){
        builder.buildWall();
        builder.buildTV();
        builder.buildSofa();
        return builder.getProduct();
    }
}
